package xujoe.cyjl.service;


import xujoe.cyjl.dao.AdminMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;


import xujoe.cyjl.pojo.Idiom;
import xujoe.cyjl.pojo.Sysdata;


@Service("AdminService")
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public ArrayList<Idiom> getNext(String nowWord) {//取出最后一个字查询
        System.out.println("---s");
        String lastStr = nowWord.substring(nowWord.length() - 1, nowWord.length());
        ArrayList<Idiom> nextWords = this.adminMapper.QueryCY(lastStr);//查询出来 排序
        int i, j, len = nextWords.size();
        for (i = 0; i < len - 1; i++)
            for (j = 0; j < len - 1 - i; j++) { /* 冒泡排序*/
                if (nextWords.get(j).getName().length() > nextWords.get(j + 1).getName().length()) { /* 相邻元素比较，若逆序则交换（升序为左大于右，降序反之） */
                    Idiom temp;
                    temp = nextWords.get(j);
                    nextWords.set(j, nextWords.get(j + 1));
                    nextWords.set(j + 1, temp);
                }
            }
        // getNextAndPinYin(nowWord);  //汉字转拼音出现一点问题
        if (nextWords.size() >= 1) {//防止非汉字查询  有效接龙数
            addFlow("成语接龙");
        }
        return nextWords;
    }

    public ArrayList<Idiom> getNextAndPinYin(String nowWord) {// 取出最后一个字的拼音 查询 获取成语拼音
        String lastStr = nowWord.substring(nowWord.length() - 1, nowWord.length());
        System.out.println(lastStr + "lastStr");
        ArrayList<Idiom> nextWord = null;
        //ArrayList<Idiom> nextWord = this.adminMapper.QueryCY(lastStr);
        return nextWord;
    }


    private void addFlow(String type) {//查询一次执行一次使用次数
        if ("成语接龙".equals(type)) {
            idiomAddOne();
        }


    }


    private void idiomAddOne() {//接龙次数增加 每天一条数据

        Sysdata sqlLastData = this.adminMapper.getLastdate();

        if (sqlLastData != null) {//第一条数据
            Date nowTime = new Date();
            GregorianCalendar lastDate = new GregorianCalendar();  //如果给的Date对象date，就忽略上句
            GregorianCalendar nowDate = new GregorianCalendar();
            lastDate.setTime(sqlLastData.getDate());
            nowDate.setTime(nowTime);
            if (lastDate.get(GregorianCalendar.YEAR) == nowDate.get(GregorianCalendar.YEAR) &&
                    lastDate.get(GregorianCalendar.MONTH) == nowDate.get(GregorianCalendar.MONTH) &&
                    lastDate.get(GregorianCalendar.DAY_OF_MONTH) == nowDate.get(GregorianCalendar.DAY_OF_MONTH)) {
                //是同一天
                this.adminMapper.idiomAddOne(String.valueOf(sqlLastData.getId()));
            } else {
                //不是同一天
                this.adminMapper.addSysData();
            }
        } else {
            this.adminMapper.addSysData();
        }
    }


    public int getAllIdiom_n() {
        ArrayList<Sysdata> allData = this.adminMapper.QueryAllIdiom_n();
        int AllIdiom_n = 0;
        for (int i = 0; i < allData.size(); i++) {
            AllIdiom_n = AllIdiom_n + allData.get(i).getIdiom_n();
        }
        return AllIdiom_n;
    }


}